const mongoose = require("mongoose");
//管理员数据接口
const get = async ({ curPage, eachPage }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: []
    }
    //model只有一个参数代表获取
    const adModel = mongoose.model("adminsModel");
    //获取当前model映射的那个集合里面存在的文档总数
    data.total = await adModel.countDocuments();
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    //exec执行populate填充
    data.rows = await adModel.find().populate("grade").skip(skipNum).limit(eachPage).sort({ _id: -1 }).exec();
    return data;

}
// 删除
const Delete = async _id => {
    const adModel = mongoose.model("adminsModel");
    await adModel.remove({ _id })
    const data = { status: true };
    return data
}
//将对应id的数据修改
const change = async ({adminName, adminPassword,phone , _id}) => {
    // 更新修改信息
    const adModel = mongoose.model("adminsModel");
    await adModel.findOneAndUpdate({ _id }, {adminName, adminPassword,phone  });
    const data = { status: true };
    return data;
}
// 获取_id对应的父类型
const getOne = async ({_id}) => {
    const adModel = mongoose.model("adminsModel");
    // 通过数据库_id拿到一条数据
    return await adModel.findOne({ _id });
}
module.exports = {
    get,
    Delete,
    change,
    getOne,


}














